package com.ican.common.hibernate;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;

public class SimpleDao {

    protected SessionFactory sessionFactory;
    
    public SimpleDao() {

    }
    
    public SimpleDao(final SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    
    //公共方法
	@Resource
    public void setSessionFactory(final SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
	public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
	public Session getSession() {
        Session session = sessionFactory.getCurrentSession();
        return session;
    }
	
    @SuppressWarnings("unchecked")
    public List<Map<String, Object>> query(final String queryString, final Object... values) {
        SQLQuery query = getSession().createSQLQuery(queryString);
        if (values != null) {
            for (int i = 0; i < values.length; i++) {
                query.setParameter(i, values[i]);
            }
        }
        return query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
    }
    @SuppressWarnings("unchecked")
    public List<Map<String, Object>> query(final String queryString, final Map<String, ?> values) {
        SQLQuery query = getSession().createSQLQuery(queryString);
        if (values != null) {
            query.setProperties(values);
        }
        return query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
    }
    @SuppressWarnings("unchecked")
    public Map<String, Object> queryUnique(final String queryString, final Object... values) {
        SQLQuery query = getSession().createSQLQuery(queryString);
        if (values != null) {
            for (int i = 0; i < values.length; i++) {
                query.setParameter(i, values[i]);
            }
        }
        return (Map<String, Object>) query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).uniqueResult();
    }
    @SuppressWarnings("unchecked")
    public Map<String, Object> queryUnique(final String queryString, final Map<String, ?> values) {
        SQLQuery query = getSession().createSQLQuery(queryString);
        if (values != null) {
            query.setProperties(values);
        }
        return (Map<String, Object>) query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).uniqueResult();
    }
    public int saveOrUpdate(final String queryString, final Object... values) {
        SQLQuery query = getSession().createSQLQuery(queryString);
        if (values != null) {
            for (int i = 0; i < values.length; i++) {
                query.setParameter(i, values[i]);
            }
        }
        return query.executeUpdate();
    }
    public int saveOrUpdate(final String queryString, final Map<String, ?> values) {
        SQLQuery query = getSession().createSQLQuery(queryString);
        if (values != null) {
            query.setProperties(values);
        }
        return query.executeUpdate();
    }
}